<?php
namespace app\api\controller;
use app\base\CommonModel;
use think\Request;
use app\base\model\{
     Goods as GoodsModel,
    IndexModule,
    GoodsImages,
    Master,
    MasterVideo,
    MasterVideoGoods,
    GoodsCate,
    GoodsComment,
    CommentImages,
    Collection,
    AttributeValues,
    Attribute,
    GoodsAttribute,
    GoodsAttrCombination
};
class Goods extends Base
{
    //商品模型
    protected $goods;
    //大师模型
    protected $master;
    //大师视频模型
    protected $masterVideo;
    //大师推荐商品模型
    protected $masterVideoGoods;
    //商品分类模型
    protected $cate;
    //商品评论模型
    protected $comment;
    //商品评论图片模型
    protected $comment_images;
    //商品图片模型
    protected $goods_images;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->goods = new GoodsModel();
        $this->master = new Master();
        $this->masterVideo = new MasterVideo();
        $this->masterVideoGoods = new MasterVideoGoods();
        $this->cate = new GoodsCate();
        $this->comment = new GoodsComment();
        $this->comment_images = new CommentImages();
        $this->goods_images = new GoodsImages();
    }
    /**
     *今日特购商品列表
     * return json
     */
    public function getTodayGoods()
    {
        $list = $this->goods->where(['goods_status' => 1, 'is_today' => 1, 'is_delete' => 0])->order('goods_id', 'desc')->limit(6)->select();
        $indexModule = new IndexModule();
        $moduleInfo = $indexModule->where(['m_id' => 1])->find();
        if (!empty($list)) {
            $msg['message'] = checkMsg(GET_DATA_SUCCESS);
            $msg['banner_path'] = $moduleInfo['banner_img'];
            $msg['goods_list'] = $list;
        } else {
            $msg['message'] = checkMsg(GET_DATA_FAILE);
            $msg['banner_path'] = $moduleInfo['banner_img'];
        }
        return $msg;
    }
    /**
     *热门商品
     * return json
     */
    public function getHotGoods()
    {
        $pageNo = $this->request->post('pageNo', 1);
        $pageSize = $this->request->post('pageSize', 5);
        $list = $this->goods->where(['goods_status' => 1, 'is_hot' => 1, 'is_delete' => 0])->order('goods_id', 'desc')->page($pageNo, $pageSize)->select();
        $indexModule = new IndexModule();
        $moduleInfo = $indexModule->where(['m_id' => 2])->find();
        if (!empty($list)) {
            $msg['message'] = checkMsg(GET_DATA_SUCCESS);
            $msg['banner_path'] = $moduleInfo['banner_img'];
            $msg['goods_list'] = $list;
        } else {
            $msg['message'] = checkMsg(GET_DATA_FAILE);
            $msg['banner_path'] = $moduleInfo['banner_img'];
        }
        return $msg;
    }
    /**
     *大师推荐商品
     * return json
     */
    public function getRecomList()
    {
        $indexModule = new IndexModule();
        $moduleInfo = $indexModule->where(['m_id' => 3])->find();
        $list = $this->masterVideoGoods->alias('vg')
            ->join('yc_goods g', 'g.goods_id=vg.gid', 'left')
            ->field("vg.*,g.goods_name,g.goods_price,g.goods_thumb,g.goods_desc,g.goods_original_price")
            ->where(['g.goods_status' => 1, 'g.is_delete' => 0])
            ->order('vg.id', 'desc')
            ->group('vg.gid')
            ->limit(4)
            ->select();
        if (!empty($list)) {
            $msg['message'] = checkMsg(GET_DATA_SUCCESS);
            $msg['banner_path'] = $moduleInfo['banner_img'];
            $msg['goods_list'] = $list;
        } else {
            $msg['message'] = checkMsg(GET_DATA_FAILE);
            $msg['banner_path'] = $moduleInfo['banner_img'];
        }
        return $msg;
    }
    /**
     *大师推荐热门商品列表
     * return json
     */
    public function getRecomHostList()
    {
        $list = $this->masterVideoGoods->alias('vg')
            ->join('yc_goods g', 'g.goods_id=vg.gid', 'left')
            ->field("vg.*,g.goods_name,g.goods_price,g.goods_thumb,g.goods_desc,g.goods_original_price")
            ->where(['g.goods_status' => 1, 'g.is_delete' => 0, 'vg.is_hot' => 1])
            ->order('vg.id', 'desc')
            ->group('vg.gid')
            ->limit(4)
            ->select();
        if (!empty($list)) {
            $msg['message'] = checkMsg(GET_DATA_SUCCESS);
            $msg['list'] = $list;
        } else {
            $msg['message'] = checkMsg(GET_DATA_FAILE);
        }
        return $msg;
    }
    /**
     *分类商品展示
     * return json
     */
    public function moduleGoodsList()
    {
        $cateList = $this->cate->alias('c')->where(['c.is_show' => 1, 'c.is_show_index_module' => 1, 'c.pid' => 0])->select();
        $cate_ids = [];
        foreach ($cateList as $key => $value) {
            array_push($cate_ids, $value['cate_id']);
            $sencod_cate = $this->cate->where(['pid' => $value['cate_id'], 'is_show' => 1])->select();
            foreach ($sencod_cate as $k => $v) {
                array_push($cate_ids, $v['cate_id']);
                $third_cate = $this->cate->where(['pid' => $v['cate_id'], 'is_show' => 1])->select();
                foreach ($third_cate as $tk => $tv) {
                    array_push($cate_ids, $tv['cate_id']);
                }
            }
            $goods_list = $this->goods->where(['goods_cate' => ['in', $cate_ids], 'goods_status' => 1, 'is_delete' => 0])->field('goods_id,goods_name,goods_price,goods_thumb,goods_desc,goods_original_price')->order('goods_id', 'desc')->limit(6)->select();
            $cateList[$key]['goods_list'] = $goods_list;
        }
        $msg['message']=checkMsg(GET_DATA_SUCCESS);
        $msg['list']=$cateList;
        return $msg;
    }
    /**
     *猜你喜欢的商品列表
     * return json
     */
    public function likeGoodsList()
    {
        $where = [
            'goods_status' => 1,
            'is_delete' => 0
        ];
        $goods_list = $this->goods->where($where)->select();
        $goods_ids = [];
        foreach ($goods_list as $key => $value) {
            $goods_ids[] = $value['goods_id'];
        }
        $rand_keys = array_rand($goods_ids, 2);
        for ($i = 0; $i < count($rand_keys); $i++) {
            $rand_goods[] = $goods_ids[$rand_keys[$i]];
        }
        $like_goods = $this->goods->where(['goods_id' => ['in', $rand_goods]])->select();
        if (!empty($like_goods)) {
            $msg['message'] = checkMsg(GET_DATA_SUCCESS);
            $msg['list'] = $like_goods;
        } else {
            $msg['message'] = checkMsg(GET_DATA_FAILE);
        }
        return $msg;
    }
    /**
     *获取指定分类下的商品
     * return json
     */
    public function getCateGoodsList()
    {
        $cate_id = $this->request->post('cate_id');
        if (empty($cate_id)) {
            $msg['message'] = checkMsg(CANSHU_ERROR);
            return $msg;
        }
        //获取改分类下的所有的子分类
        $cateInfo = $this->cate->where(['cate_id' => $cate_id, 'is_show' => 1])->find();
        if (empty($cateInfo)) {
            $msg['message'] = checkMsg(CATE_NOT_EXIT);
            return $msg;
        }
        $cate_ids[] = $cate_id;
        if ($cateInfo['level'] == 1) {  //该分类为一级分类
            $second_cate = $this->cate->where(['pid' => $cate_id])->select();
            foreach ($second_cate as $key => $value) {
                $cate_ids[] = $value['cate_id'];
                $third_cate = $this->cate->where(['pid' => $value['cate_id']])->select();
                foreach ($third_cate as $k => $v) {
                    $cate_ids[] = $v['cate_id'];
                }
            }
        } elseif ($cateInfo['level'] == 2) { //该分类为二级分类
            $third_cate = $this->cate->where(['pid' => $cate_id])->select();
            foreach ($third_cate as $k => $v) {
                $cate_ids[] = $v['cate_id'];
            }
        }
        $goods_list = $this->goods->where(['goods_status' => 1, 'is_delete' => 0, 'goods_cate' => ['in', $cate_ids]])->order('goods_id', 'desc')->group('goods_id')->select();
        if (!empty($goods_list)) {
            $msg['message'] = checkMsg(GET_DATA_SUCCESS);
            $msg['cate_name']=$cateInfo['cate_name'];
            $msg['list'] = $goods_list;
        } else {
            $msg['message'] = checkMsg(GET_DATA_FAILE);
            $msg['cate_name']=$cateInfo['cate_name'];
        }
        return $msg;
    }
    /**
     *获取商品详情
     *return json
     */
    public function getGoodsInfo()
    {
        $goods_id = $this->request->post('goods_id');
        if (empty($goods_id)) {
            $msg['message'] = checkMsg(CANSHU_ERROR);
            return $msg;
        }
        $goodsInfo = $this->goods->where(['goods_id' => $goods_id, 'goods_status' => 1, 'is_delete' => 0])->find();
        if (empty($goodsInfo)) {
            $msg['message'] = checkMsg(GOODS_NOT_EXIT);
            return $msg;
        }
        $is_purchase=0;
        if($goodsInfo['is_today']==1){
           $is_purchase=1;
        }else{
            $count=$this->masterVideoGoods->where(['gid'=>$goodsInfo['goods_id']])->count();
            if($count>0){
                $is_purchase=1;
            }
        }
       $goodsInfo['is_purchase']=$is_purchase; //判断商品是否是限购
       $videoInfo=$this->masterVideo->where(['v_id'=>$goodsInfo['video_id'],'status'=>1])->find();
       if(!empty($videoInfo)){  //商品的介绍视频
           $goodsInfo['goods_video']=$videoInfo;
       }else{
           $goodsInfo['goods_video']=[];
       }
        //该商品的评论
         $comment_list=$this->comment->where(['goods_id'=>$goods_id,'star_num'=>5])->order('id','desc')->select();
         foreach($comment_list as $key=>$value){
             $comment_image=$this->comment_images->where(['comment_id'=>$value['id']])->select();
             $image_path=[];
             foreach($comment_image as $k=>$v){
                 $image_path[]=$v['img_path'];
             }
             $comment_list[$key]['image_path']=$image_path;
             $comment_list[$key]['add_time']=date('Y-m-d H:i:s',$value['add_time']);
         }
         $comment_count=count($comment_list);
         $goodsInfo['comment_count']=$comment_count;
         $goodsInfo['comment_list']=$comment_list;
         //该商品的样式
        $goodsAttrCom = new GoodsAttrCombination();
        $goodsAttr    = new GoodsAttribute();
        $attrValue    = new AttributeValues();
        $goods_attr=$goodsAttrCom->where(['goods_id'=>$goods_id])->select();
        foreach($goods_attr as $key=>$value){
            $value_ids=explode(',',$value['values_id']);
            $attr_value_name=[];
            for($i=0;$i<count($value_ids);$i++){
                $valueInfo=$attrValue->where(['id'=>$value_ids[$i]])->find();
                $attr_value_name[]=$valueInfo['value'];
            }
            $goods_attr[$key]['attr_list']=$attr_value_name;
        }
         $goodsInfo['stype']=$goods_attr;
        $image_list = $this->goods_images->where(['goods_id'=>$goods_id])->field('img_id,img_path')->select();
        $goodsInfo['image_list']=$image_list;
         $msg['message']=checkMsg(GET_DATA_SUCCESS);
         $msg['info']=$goodsInfo;
        return $msg;
    }
  /**
   *收藏商品
   * return json
   */
  public function collectGoods(){
      if(empty($this->userInfo)){
          $msg['message']=checkMsg(LOGIN_ERROR);
          return $msg;
      }
      $goods_id = $this->request->post('goods_id');
      if(empty($goods_id)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $collectModel=new Collection();
      $count=$collectModel->where(['user_id'=>$this->user_id,'goods_id'=>$goods_id])->count();
      if($count>0){
          $msg['message']=checkMsg(CANNOT_COLLECT_RE);
          return $msg;
      }
      $data=[
          'user_id'  =>$this->user_id,
          'goods_id' =>$goods_id,
          'add_time' =>time()
      ];
      $res=$collectModel->insertGetId($data);
      if($res){
          $msg['message']=checkMsg(COLLECT_SUCCESS);
      }else{
          $msg['message']=checkMsg(COLLECT_FAIL);
      }
      return $msg;
  }
  /**
   *搜索商品
   * return json
   */
  public function searchGoods(){
      $keywords = $this->request->post('keywords');
      if(empty($keywords)){
          $msg['message']=checkMsg(CANSHU_ERROR);
          return $msg;
      }
      $where=[
          'goods_name' => ['like',"%$keywords%"],
          'goods_status'=>1,
          'is_delete'   =>0
      ];
      $goods_list=$this->goods->where($where)->group('goods_id')->order('goods_id','desc')->select();
      if(!empty($goods_list)){
          $msg['message']=checkMsg(GET_DATA_SUCCESS);
          $msg['list'] = $goods_list;
      }else{
          $msg['message']=checkMsg(GET_DATA_FAILE);
      }
      return $msg;
  }
}